package hjj.dwr;

import java.util.ArrayList;
import java.util.List;

import dao.impl.DeptDAOImpl;
import dao.impl.EmpDAOImpl;
import dao.impl.EmpTransferDAOImpl;
import dao.impl.ResumeDAOImpl;
import dao.impl.dept_duty_empDAOImpl;

import model.Dept;

public class DeptCombine {
	public List<Dept> getAllDept() {
		List<Dept> list = new ArrayList<Dept>();
		DeptDAOImpl ddaoi = new DeptDAOImpl();
		list = ddaoi.getALLDept();
		return list;
	}

	public Dept getDeptById(int deptid) {
		Dept dept = new Dept();
		DeptDAOImpl ddaoi = new DeptDAOImpl();
		dept = ddaoi.getDeptById(deptid);
		return dept;
	}

	public boolean addDept(String deptid, String deptname, String headerid,
			String status) {
		boolean flag = false;
		try {
			int addeptid = Integer.parseInt(deptid);
			int adheaderid = Integer.parseInt(headerid);
			DeptDAOImpl ddaoi = new DeptDAOImpl();
			Dept deptheader = new Dept();
			deptheader = ddaoi.getDeptHeader(adheaderid);
			Dept dept = new Dept();
			dept.setId(addeptid);
			dept.setName(deptname);
			dept.setDept(deptheader);
			if ("1".equals(status)) {
				dept.setStatus(true);
			} else {
				dept.setStatus(false);
			}
			flag = ddaoi.addDept(dept);
		} catch (Exception e) {
		}
		return flag;
	}
	
	public boolean combineDept(String deptid, String deptname,String deptaid,String deptbid, String headerid,
			String status) {
		boolean flag = false;
		EmpDAOImpl empdaoimpl=new EmpDAOImpl();
		DeptDAOImpl deptdaoimpl = new DeptDAOImpl();
		ResumeDAOImpl resumedaoimpl=new ResumeDAOImpl();
		dept_duty_empDAOImpl d_d_e=new dept_duty_empDAOImpl();
		EmpTransferDAOImpl emptransferdaoimpl=new EmpTransferDAOImpl();
		try {
			if(!this.addDept(deptid, deptname, headerid, status)){
				return false;
			}
			int codeptid = Integer.parseInt(deptid);
			int coheaderid = Integer.parseInt(headerid);
			int codeptaid = Integer.parseInt(deptaid);
			int codeptbid = Integer.parseInt(deptbid);
			flag=empdaoimpl.updateEmpByDept(codeptaid, codeptid);
			
			flag=empdaoimpl.updateEmpByDept(codeptbid, codeptid);

			
			flag=resumedaoimpl.updateByDept(codeptaid, codeptid);
			flag=resumedaoimpl.updateByDept(codeptbid, codeptid);
			
			flag=deptdaoimpl.updateByHeader(codeptaid, codeptid);
			flag=deptdaoimpl.updateByHeader(codeptbid, codeptid);
			
			flag=emptransferdaoimpl.updateByBeforeDept(codeptaid, codeptid);
			flag=emptransferdaoimpl.updateByBeforeDept(codeptbid, codeptid);
			flag=emptransferdaoimpl.updateByAfterDept(codeptaid, codeptid);
			flag=emptransferdaoimpl.updateByAfterDept(codeptbid, codeptid);
			
			flag=d_d_e.updateByDept(codeptaid, codeptid);
			flag=d_d_e.updateByDept(codeptbid, codeptid);
			
			flag=deptdaoimpl.deleteDeptById(codeptaid);	
			flag=deptdaoimpl.deleteDeptById(codeptbid);
			
			flag=true;
		} catch (Exception e) {
		}
		
		return flag;
	}

	public boolean updateDept(String deptid, String deptname, String headerid,
			String status) {
		boolean flag = false;
		try {
			int updeptid = Integer.parseInt(deptid);
			int upheaderid = Integer.parseInt(headerid);
			DeptDAOImpl ddaoi = new DeptDAOImpl();
			Dept deptheader = new Dept();
			deptheader = ddaoi.getDeptHeader(upheaderid);
			Dept dept = new Dept();
			dept.setId(updeptid);
			dept.setName(deptname);
			dept.setDept(deptheader);
			if ("1".equals(status)) {
				dept.setStatus(true);
			} else {
				dept.setStatus(false);
			}
			flag = ddaoi.updateDept(dept);
		} catch (Exception e) {
		}
		return flag;
	}
	public boolean deleteDept(String deptid) {
		boolean flag = false;
		try {
			int dedeptid = Integer.parseInt(deptid);
			DeptDAOImpl ddaoi = new DeptDAOImpl();
			flag = ddaoi.deleteDeptById(dedeptid);
		} catch (Exception e) {
		}
		return flag;
	}
}